package cn.itcast.bos.action.base;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.IAreaService;
import cn.itcast.bos.utils.PinYin4jUtils;

/** 
 * 区域的控制层
 *
 * @author Shao-Fei Fan 
 * @version 1.0,2017年9月5日 上午8:46:44 
 */
@Controller
@Scope(value="prototype")
@ParentPackage("json-default")
@Namespace("/")
public class AreaAction extends BaseAction<Area> {
	
	@Autowired
	private IAreaService areaService;
	
	// 用于接收上传文件，因为只需要将其解析并上传数据至数据库所以并不需要类型及名称
	private String file;

	public void setFile(String file) {
		this.file = file;
	}
	
	@Action(value="area_upload",results={@Result(name="success",location="./pages/base/area.html",type="redirect")})
	public String fileupload() throws IOException{
		// 创建list用来存储解析出的area数据
		List<Area> areas = new ArrayList<Area>();
		
		// 创建excel工作区间
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file));
		
		// 获得工作簿
		HSSFSheet sheet = workbook.getSheetAt(0);
		
		// 获得每行数据
		for (Row row : sheet) {
			// 跳过第一行
			if (row.getRowNum()==0) {
				continue;
				
			// 跳过空行
			}else if ((row.getCell(0)==null)||(StringUtils.isBlank(row.getCell(0).getStringCellValue()))) {
				continue;
			}else{
				//依据 区域编号	省份	城市	区域	邮编信息生成area
				Area area = new Area();
				area.setId(row.getCell(0).getStringCellValue());
				area.setProvince(row.getCell(1).getStringCellValue());
				area.setCity(row.getCell(2).getStringCellValue());
				area.setDistrict(row.getCell(3).getStringCellValue());
				area.setPostcode(row.getCell(4).getStringCellValue());
				
				// 基于pinyin4j生成城市编码和简码
				String province = area.getProvince();
				String city = area.getCity();
				String district = area.getDistrict();
				
				province = province.substring(0, province.length()-1);
				city = city.substring(0, city.length()-1);
				district = district.substring(0, district.length()-1);
				
				
				// 简码
				String[] headArray = PinYin4jUtils.getHeadByString(province+city+district);
				StringBuffer buffer = new StringBuffer();
				
				for (String string : headArray) {
					buffer.append(string);
				}
				String shortcode = buffer.toString();
				area.setShortcode(shortcode);
				
				// 城市编码
				String cityCode = PinYin4jUtils.hanziToPinyin(city,"");
				area.setCitycode(cityCode);
				
				// 将信息存储进list集合
				areas.add(area);			
				}
		}
		
		// 调用Service层
		areaService.addAreas(areas);
		return SUCCESS;
	}
	
	@Action(value="area_findAll",results={@Result(name="success", type="json")})
	public String areaSearch(){
		// 分页查询条件加入
		Pageable pageable = new PageRequest(page-1,rows);
		
		// 封装条件
		Specification<Area> specification = new Specification<Area>() {
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				// 省、城市、区县
				List<Predicate> list = new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(model.getProvince())){
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+model.getProvince()+"%");
					list.add(p1);
				}
				if(StringUtils.isNotBlank(model.getCity())){
					Predicate p2 = cb.like(root.get("city").as(String.class), "%"+model.getCity()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(model.getDistrict())){
					Predicate p2 = cb.like(root.get("district").as(String.class), "%"+model.getDistrict()+"%");
					list.add(p2);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		// 调用service
		Page<Area> page = areaService.areaSearch(pageable,specification);
		
		pushPageDataToVolueStake(page);
		return SUCCESS;
	}
	
	

}
